package com.mjw.tender.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.mjw.tender.dto.FileInfo;
import com.mjw.tender.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@RestController
@RequestMapping(value = "upload")
@Slf4j
public class UploadController {


    private static final String ENDPOINT = "oss-cn-shenzhen.aliyuncs.com";
    private static final String ACCESS_KEY_ID = "LTAIUEM4x1YOqT0O";
    private static final String ACCESS_KEY_SECRET = "XVRECYNWqS7uzssIXeNrcgKIamBjTh";
    private static final String BUCKET_NAME = "javasite";

    /**
     * 文件上传
     *
     * @param multipartFile @{code MultipartFile}
     */
    @PostMapping(value = "/oss")
    public ResponseResult<FileInfo> ossUpload(MultipartFile multipartFile) {
        String fileName = multipartFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newName = UUID.randomUUID() + "." + suffix;

        OSS client = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        try {
            client.putObject(new PutObjectRequest(BUCKET_NAME, newName, new ByteArrayInputStream(multipartFile.getBytes())));
            // 上传文件路径 = http://BUCKET_NAME.ENDPOINT/自定义路径/fileName
            FileInfo fileInfo = new FileInfo();
            fileInfo.setPath("http://" + BUCKET_NAME + "." + ENDPOINT + "/" + newName);
            return new ResponseResult<FileInfo>(ResponseResult.CodeStatus.OK, "文件上传成功", fileInfo);
        } catch (IOException e) {
            return new ResponseResult<FileInfo>(ResponseResult.CodeStatus.FAIL, "文件上传失败，请重试");
        } finally {
            client.shutdown();
        }
    }


    @Value("${upload.path}")
    private String FILE_PAHT;
    @PostMapping("")
    public ResponseResult<FileInfo> upload(@RequestParam("file")MultipartFile multipartFile){

        String fileName = multipartFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newName = UUID.randomUUID() + "." + suffix;

        File targetFile = new File(FILE_PAHT);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        try {
            OutputStream out = new FileOutputStream(FILE_PAHT+newName);
            out.write(multipartFile.getBytes());
            FileInfo fileInfo = new FileInfo();
            fileInfo.setPath(FILE_PAHT+fileName);
            fileInfo.setFileName(newName);
            return new ResponseResult(ResponseResult.CodeStatus.OK,fileInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(ResponseResult.CodeStatus.FAIL);
        }
    }

}
